"""
测试返回内容
"""
import time
import requests
import json


def get_res_info(remark_result, info_str, id):
    res_info = remark_result[info_str]
    has_error = False
    if len(res_info) <= 0:
        print(f"{info_str} 异常id={id}")
        has_error = True
    return has_error

def check_request_result_str(id, is_local_server=False):
    "http://10.43.125.170:9840/article/remark/result"
    url = "http://10.43.125.170:9840/article/remark/result" if is_local_server else "http://10.43.125.170:9840/article/remark/result"
    print("url={}".format(url))

    payload = json.dumps({
      "id": id
    })
    headers = {
      'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    if response.status_code == 200:
        try:
            info = json.loads(response.text)
            status = info["data"]["status"]
            if status == 1:
                print(f"id={id} 作文评测中，等等10s")
                time.sleep(10)
            elif status == 2:
                print(f"id={id} 作文结束")
                remark_result = info["data"]["remark_result"]
                get_res_info(remark_result, "thematic", id)
                get_res_info(remark_result, "content", id)
                get_res_info(remark_result, "structure", id)
                get_res_info(remark_result, "langPerformance", id)
                get_res_info(remark_result, "write", id)

                get_res_info(remark_result, "evaluate", id)
                get_res_info(remark_result, "suggest", id)

                score_info = remark_result["score"]
                if len(score_info.get("s1", "")) <= 0 or len(score_info.get("s2", "")) <= 0 or len(score_info.get("s3", "")) <= 0 or len(score_info.get("s4", "")) <= 0 or len(score_info.get("s5", "")) <= 0 or len(score_info.get("all_score", "")) <= 0:
                    print(f"分数异常id={id}")
            elif status == 3:
                print(f"id={id} 评测失败")
            else:
                print(f"异常 id={id} ：{response.text}")
        except Exception as e:
            print(f"json 异常 id={id} :{e}\n{response.text}")
    return json.loads(response.text)


if __name__ == '__main__':

    # article_id = 700
    # check_request_result_str(article_id, is_local_server=False)

    for article_id in range(2335, 2344):
        check_request_result_str(article_id, is_local_server=False)